package com.ama.controller;



import com.ama.dao.StaffDao;
import com.ama.pojo.Staff;
import com.ama.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@Controller
public class PasswordUpdate {
    @Autowired
    StaffService staffService;

    @RequestMapping("/updatePsw")
    public String PasswordUpdate(HttpServletRequest request, HttpSession session) {
        Staff staff = (Staff) session.getAttribute("user");
        String newpwd = request.getParameter("newpwd");
        System.out.println(newpwd);
        String oldpwd = request.getParameter("oldpwd");

        String confirmpwd = request.getParameter("confirmpwd");
        if (oldpwd.equals(staff.getStaffPassword())) {
            if (newpwd.equals(confirmpwd)) {
                Staff newStaff = staffService.selectById(staff.getStaffId());
                newStaff.setStaffPassword(newpwd);
                staffService.updateByIdSelective(newStaff);
                session.setAttribute("user", newStaff);
                return "redirect:/success/updatePsw_suc.jsp";
            } else {
                return "redirect:/error/updatePsw_err.jsp";
            }

        } else {
            return "redirect:/error/updatePsw_err.jsp";
        }
    }
}











